package com.yuncheng.oaApi.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuncheng.annotation.AutoLog;
import com.yuncheng.common.base.controller.BaseController;
import com.yuncheng.common.query.QueryBuilder;
import com.yuncheng.oaApi.constant.OaBusinessConstant;
import com.yuncheng.oaApi.constant.OaLgqsOaFlowConstants;
import com.yuncheng.oaApi.entity.NkJcxswsq;
import com.yuncheng.oaApi.entity.OaGwyc;
import com.yuncheng.oaApi.service.INkJcxswsqService;
import com.yuncheng.oaApi.service.IOaGwycService;
import com.yuncheng.spcyApi.utils.GetUuIdUtils;
import com.yuncheng.spcyApi.utils.MyExcelUtils;
import com.yuncheng.utils.ThreadContextHelper;
import com.yuncheng.vo.CurrentUserVo;
import com.yuncheng.vo.HttpResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import utils.DateConversionUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @Description: 经常性事务申请
 * @Author: lhb
 * @Date: 2023-06-30
 * @Version: V1.0
 */
@Api(tags = "经常性事务申请")
@RestController
@RequestMapping("/api/spzx-oa/NkJcxswsq")
public class NkJcxswsqController extends BaseController<OaGwyc, IOaGwycService> {

    private static final Logger log = LoggerFactory.getLogger(NkJcxswsqController.class);

    @Resource
    private INkJcxswsqService oaJcxswsqService;



    /**
     * 经常性事务申请
     *
     * @param nkJcxswsq
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "经常性事务申请-分页列表查询")
    @ApiOperation(value = "经常性事务申请-分页列表查询")
    @GetMapping(value = "/list")
    public HttpResult<?> queryPageList(NkJcxswsq nkJcxswsq, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo, @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) {
        QueryWrapper<NkJcxswsq> queryWrapper = QueryBuilder.initQueryWrapper(nkJcxswsq, req.getParameterMap());

        Page<NkJcxswsq> page = new Page<>(pageNo, pageSize);
        IPage<NkJcxswsq> pageList = oaJcxswsqService.page(page, queryWrapper);

        return HttpResult.ok(pageList);
    }


    @AutoLog(value = "经常性事务申请-新增")
    @ApiOperation(value = "经常性事务申请-新增")
    @PostMapping(value = "/add")
    public HttpResult<?> add(@RequestBody NkJcxswsq nkJcxswsq) {
        CurrentUserVo currentUser = ThreadContextHelper.getCurrentUser();
        String id = GetUuIdUtils.ReplaceUuId();
        nkJcxswsq.setId(id);
        nkJcxswsq.setFsfysq("未申请");
        nkJcxswsq.setFsqbm(currentUser.getOrgName());
        nkJcxswsq.setFsqr(currentUser.getName());
        nkJcxswsq.setFsqrid(currentUser.getId());
        nkJcxswsq.setFsqsj(DateConversionUtils.DateToDays(new Date()));

        boolean isSaved = oaJcxswsqService.save(nkJcxswsq);

        // 如果保存成功，从数据库中获取最新的对象
        if (isSaved) {
            NkJcxswsq updatedNkJcxswsq = oaJcxswsqService.getById(id);
            return HttpResult.ok(updatedNkJcxswsq);
        } else {
            // 如果保存失败，可以根据具体情况进行处理
            return HttpResult.error("新增失败");
        }
    }


    @AutoLog(value = "经常性事务申请-新增")
    @ApiOperation(value = "经常性事务申请-新增")
    @PutMapping(value = "/edit")
    public HttpResult<?> edit(@RequestBody NkJcxswsq nkJcxswsq) {
        boolean isUpdated = oaJcxswsqService.updateById(nkJcxswsq);
        if (isUpdated) {

            NkJcxswsq updatedNkJcxswsq = oaJcxswsqService.getById(nkJcxswsq.getId());
            return HttpResult.ok(updatedNkJcxswsq);
        } else {

            return HttpResult.error("更新失败");
        }
    }







    @AutoLog(value = "经常性事务申请-删除")
    @ApiOperation(value = "经常性事务申请-删除")
    @GetMapping(value = "/delete")
    public HttpResult<?> delete(String id) {

        //如果这条记录以及被报销。还能不能删除
        oaJcxswsqService.removeById(id);
        return HttpResult.ok("删除成功");
    }





}
